package kr.or.kosta.otr.service.boardservice;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.annotation.Resource;

import kr.or.kosta.otr.persistence.board.BoardDao;
import kr.or.kosta.otr.persistence.comment.CommentDao;
import kr.or.kosta.otr.persistence.file.FileDao;
import kr.or.kosta.otr.utill.BoardListVO;
import kr.or.kosta.otr.utill.PagingBean;
import kr.or.kosta.otr.vo.BoardVO;
import kr.or.kosta.otr.vo.CommentVO;
import kr.or.kosta.otr.vo.FileVO;

import org.springframework.stereotype.Service;

@Service("BoardService")
public class BoardServiceImpl implements BoardService{
	@Resource(name="BoardDao")
	BoardDao boardDao;
	
	@Resource(name="FileDao")
	FileDao fileDao;
	
	@Resource(name="CommentDao")
	CommentDao commentDao; 
	
	@Override
	public BoardVO writeBoard(BoardVO bvo, FileVO fvo) throws SQLException {
		int boardId=boardDao.writeBoard(bvo);
		if(!fvo.getFileName().equals("No file.")){
			fileDao.uploadFile(fvo);
		}
		return boardDao.getOneBoardById(boardId);
	}

	@Override
	public void updateBoard(BoardVO bvo, FileVO fvo) throws SQLException {
		boardDao.updateBoard(bvo);
		int boardId=bvo.getBoardId();
		if(fileDao.getOneFileByBoardId(boardId)==null){
			fileDao.uploadFile(fvo);
		}else{
			fileDao.updateFile(fvo);
		}
	}

	@Override
	public void deleteBoard(int boardId) throws SQLException {
		boardDao.deleteBoard(boardId);
		fileDao.deleteFileByBoardId(boardId);
	}

	@Override
	public void deleteFile(int boardId) throws SQLException {
		fileDao.deleteFileByBoardId(boardId);
	}
	
	@Override
	public String getOneFileName(int boardId) throws SQLException {
		return fileDao.getOneFileName(boardId);
	}
	
	@Override
	public HashMap<String, Object> getOneBoardById(int boardId) throws SQLException {
		boardDao.updateHits(boardId);
		BoardVO bvo=boardDao.getOneBoardById(boardId);
		FileVO fvo=fileDao.getOneFileByBoardId(boardId);
		ArrayList<CommentVO> commentList=commentDao.getAGroupOfComments(boardId);
		
		HashMap<String, Object> map=new HashMap<String, Object>();
		map.put("bvo", bvo);
		map.put("fvo", fvo);
		map.put("commentList", commentList);
		return map;
	}
	
	@Override
	public HashMap<String, Object> getOneBoardRefresh(int boardId) throws SQLException {
		BoardVO bvo=boardDao.getOneBoardById(boardId);
		FileVO fvo=fileDao.getOneFileByBoardId(boardId);
		ArrayList<CommentVO> commentList=commentDao.getAGroupOfComments(boardId);
		
		HashMap<String, Object> map=new HashMap<String, Object>();
		map.put("bvo", bvo);
		map.put("fvo", fvo);
		map.put("commentList", commentList);
		return map;
	}

	@Override
	public BoardListVO getBoardListByPage(String pageNo) throws SQLException {
		int pn=1;
		
		if(pageNo!=null){
			pn=Integer.parseInt(pageNo);
		}
		ArrayList<BoardVO> list=boardDao.getBoardListByPage(pn);
		for (int i = 0; i < list.size(); i++) {
			int boardId=list.get(i).getBoardId();
			String fileExist=Integer.toString(fileDao.fileExist(boardId));
			list.get(i).setFileExist(fileExist);
		}
		int totalContents=boardDao.getTotalCount();
		PagingBean pb=new PagingBean(totalContents, pn);
		
		return new BoardListVO(list, pb);
	}

	@Override
	public ArrayList<BoardVO> getBoardByUserId(String userId) throws SQLException {
		ArrayList<BoardVO> list=boardDao.getBoardByUserId(userId);
		for (int i = 0; i < list.size(); i++) {
			int boardId=list.get(i).getBoardId();
			String fileExist=Integer.toString(fileDao.fileExist(boardId));
			list.get(i).setFileExist(fileExist);
		}
		return list;
	}

	@Override
	public ArrayList<BoardVO> getBoardByTitle(String title) throws SQLException {
		ArrayList<BoardVO> list=boardDao.getBoardByTitle(title);
		for (int i = 0; i < list.size(); i++) {
			int boardId=list.get(i).getBoardId();
			String fileExist=Integer.toString(fileDao.fileExist(boardId));
			list.get(i).setFileExist(fileExist);
		}
		return list; 
	}

	@Override
	public ArrayList<BoardVO> getBoardByContent(String content) throws SQLException {
		ArrayList<BoardVO> list=boardDao.getBoardByContent(content);
		for (int i = 0; i < list.size(); i++) {
			int boardId=list.get(i).getBoardId();
			String fileExist=Integer.toString(fileDao.fileExist(boardId));
			list.get(i).setFileExist(fileExist);
		}
		return list; 
	}

	@Override
	public void writeParentComment(CommentVO cvo) throws SQLException {
		int boardId=cvo.getBoardVo().getBoardId();
		cvo.setRef(commentDao.getMaxRef(boardId)+1);
		commentDao.writeComment(cvo);
	}

	@Override
	public void writeChildComment(int parentCommentId, CommentVO childCvo) throws SQLException {
		CommentVO parentCvo=commentDao.getOneComment(parentCommentId);
		int boardId=parentCvo.getBoardVo().getBoardId();
		int ref=parentCvo.getRef();
		int step=parentCvo.getStep()+1;
		int jump=parentCvo.getJump()+1;
		int root=parentCvo.getCommentId();
		childCvo.getBoardVo().setBoardId(boardId);
		childCvo.setRef(ref);
		childCvo.setStep(step);
		childCvo.setJump(jump);
		childCvo.setRoot(root);
		commentDao.updateStep(parentCvo);
		commentDao.writeComment(childCvo);
		commentDao.updateDepth(parentCommentId);
		
	}
	
	@Override
	public void deleteComment(int CommentId) throws SQLException {
		commentDao.deleteComment(CommentId);
	}
	
	@Override
	public void updateComment(CommentVO vo) throws SQLException {
		commentDao.updateComment(vo);
	}
}
